let fs = require('fs');
const { baseurl } = require('../config/config')
module.exports = {
  async imgUpload(ctx, next) {
    let work_data = ctx.request.files.file
    let resultList = [];
    if (!Array.isArray(work_data)) {
      //单文件上传容错
      work_data = [work_data];
    }
    work_data &&
      work_data.forEach(filedata => {
        let path = filedata.path.replace(/\\/g, '/');
        let result_url = ''
        if (filedata.size > 0 && path) {
          //得到扩展名
          let extArr = filedata.name.split('.');//原文件名称['weixintupian','.jpg']
          let ext = extArr[extArr.length - 1]; //.jpg
          let nextPath = path + '.' + ext; 
          //重命名文件
          fs.renameSync(path, nextPath);//给文件加上后缀名
          result_url = baseurl + nextPath.slice(nextPath.lastIndexOf('/') + 1)
          resultList.push(result_url)
        }
      });
    ctx.body = {
      success: true,
      data: resultList
    }
  }
};
